import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import store from './store'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach((to, from, next) => {
  NProgress.start()
  // 判断是否有token
  const token = getToken()
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      // 判断是否有用户信息  没有就获取
      if (!store.getters.userId) store.dispatch('user/getUserInfoAction')
      next()
    }
  } else {
    if (whiteList.includes(to.path)) next()
    else next('/login')
  }
  document.title = getPageTitle()
  NProgress.done()
})

router.afterEach(() => {
  NProgress.done()
})
